package com.etc.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


import com.etc.controller.DamageProcess;
import com.etc.entity.Role;


public class PrintPerson {

	DamageProcess dp = new DamageProcess();

	public void placeComponents(Mypanel mp, Role role, Role enemy, JFrame jFrame, DataOutputStream dos, DataInputStream dis) {
		
		mp.setLayout(null);
		JButton t1 = new JButton(role.getCareer().getSkills().get(0).getSkillName());
		t1.setBounds(220, 400, 130, 25);
		JButton t2 = new JButton(role.getCareer().getSkills().get(1).getSkillName());
		t2.setBounds(370, 400, 130, 25);
		JButton t3 = new JButton(role.getCareer().getSkills().get(2).getSkillName());
		t3.setBounds(520, 400, 130, 25);
		JButton t4 = new JButton(role.getCareer().getSkills().get(3).getSkillName());
		t4.setBounds(670, 400, 130, 25);
		JButton t5 = new JButton(role.getCareer().getSkills().get(4).getSkillName());
		t5.setBounds(820, 400, 130, 25);
		
		mp.add(t1);
		mp.add(t2);
		mp.add(t3);
		mp.add(t4);
		mp.add(t5);
	
		flushAttr(mp, role, enemy);

		JTextArea info = new JTextArea();
		info.setFont(new Font("楷体", Font.BOLD, 16));
		info.setForeground(Color.DARK_GRAY);
		
		JScrollPane scroll = new JScrollPane(info);   //添加滚动条
		scroll.setBounds(25, 440, 1125, 300);
		//默认的设置是超过文本框才会显示滚动条，以下设置让滚动条一直显示
		scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		mp.add(scroll);

		//从服务器端读取消息
		 class MessageReader implements Runnable {
			    private boolean keepListening = true;
			    public void run() {
			      while(keepListening == true) {
			        try {
			          String nextLine = dis.readUTF();
			          String[] split = nextLine.split("#");
			          if(split[0].equals(role.getRoleId())) {
			        	new Thread(() -> mp.changeLeft()).start();    //手部动作
			          }else {
			        	new Thread(() -> mp.changeRight()).start();    //手部动作
			          }
						for(int i=1; i < split.length; i++) {
							if(!split[i].equals("")) {
								info.append(split[i]+"\n");
							}
						}
			        }
			        catch(Exception e) {
			          keepListening = false;
			          e.printStackTrace();
			        }
			      }
			    }
		 }
		 new Thread(new MessageReader()).start(); //启动此获取信息
		
		//监听五个按钮
		t1.addActionListener(e -> {
			String str = "useSkill#" + role.getRoleId() + "#0"; 
			sendData(str, mp, dos, dis);
			
			dp.useSkillMsg(role, enemy, 0, 2);
			doOperation(mp, role, t1, t2, t3, t4, t5, info, enemy);
		});
		t2.addActionListener(e -> {
			String str = "useSkill#" + role.getRoleId() + "#1"; 
			sendData(str, mp, dos, dis);
		
			dp.useSkillMsg(role, enemy, 1, 2);
			doOperation(mp, role, t1, t2, t3, t4, t5, info, enemy);
		});
		t3.addActionListener(e -> {
			String str = "useSkill#" + role.getRoleId() + "#2"; 
			sendData(str, mp, dos, dis);
			
			dp.useSkillMsg(role, enemy, 2, 2);
			doOperation(mp, role, t1, t2, t3, t4, t5, info, enemy);
		});
		t4.addActionListener(e -> {
			String str = "useSkill#" + role.getRoleId() + "#3"; 
			sendData(str, mp, dos, dis);
			
			dp.useSkillMsg(role, enemy, 3, 2);
			doOperation(mp, role, t1, t2, t3, t4, t5, info, enemy);
		});
		t5.addActionListener(e -> {
			String str = "useSkill#" + role.getRoleId() + "#4"; 
			sendData(str, mp, dos, dis);
			
			dp.useSkillMsg(role, enemy, 4, 2);
			doOperation(mp, role, t1, t2, t3, t4, t5, info, enemy);
		});
		
	}

	//打印属性
	public void flushAttr(Mypanel mp, Role role, Role enemy) {
		JTextArea left = new JTextArea();
		left.setBounds(20, 20, 180, 410);
		left.setFont(new Font("楷体", Font.BOLD, 16));
		left.append("\n    昵称:"+role.getNickname()+"\n\n");
		left.append("    职业名:"+role.getCareer().getClassify().getClassName()+"\n\n");
		left.append("    生命值:"+role.getHeal()+"\n\n");
		left.append("    魔法值:"+role.getImage()+"\n\n");
		left.append("    攻击值:"+role.getAttack()+"\n\n");
		left.append("    暴击值:"+role.getCritical()+"\n\n");
		left.append("    防御值:"+role.getDefense()+"\n\n");
		left.append("    速度值:"+role.getSpeed()+"\n\n");
		mp.add(left);
		
		
		JTextArea right = new JTextArea();
		right.setBounds(970, 20, 180, 410);
		right.setFont(new Font("楷体", Font.BOLD, 16));
		right.append("\n    昵称:"+enemy.getNickname()+"\n\n");
		right.append("    职业名:"+enemy.getCareer().getClassify().getClassName()+"\n\n");
		right.append("    生命值:"+enemy.getHeal()+"\n\n");
		right.append("    魔法值:"+enemy.getImage()+"\n\n");
		right.append("    攻击值:"+enemy.getAttack()+"\n\n");
		right.append("    暴击值:"+enemy.getCritical()+"\n\n");
		right.append("    防御值:"+enemy.getDefense()+"\n\n");
		right.append("    速度值:"+enemy.getSpeed()+"\n\n");
		mp.add(right);
	}

	//游戏操作
	public void doOperation(Mypanel mp, Role role, JButton t1, JButton t2, JButton t3, JButton t4, JButton t5,
			JTextArea info, Role enemy) {
		outputFightMsg(mp, role, info, enemy);
		inSkillCd(role, t1, t2, t3, t4, t5);
		//游戏结束
		gameOver(mp, role, enemy);
	}
	
	//游戏结束提示
	public void gameOver(Mypanel mp, Role role, Role enemy) {
		if(enemy.getHeal()==0) {
			JOptionPane.showMessageDialog(mp, role.getNickname()+"获得胜利", "游戏结束", JOptionPane.ERROR_MESSAGE);
		}
		else if(role.getHeal()==0) {
			JOptionPane.showMessageDialog(mp, enemy.getNickname()+"获得胜利", "游戏结束", JOptionPane.ERROR_MESSAGE);
		}
	}

	//进入技能CD，设置JButton不可见
	public void inSkillCd(Role role, JButton t1, JButton t2, JButton t3, JButton t4, JButton t5) {
		if(role.getCareer().getSkills().get(0).getAvailability() != 0) {
			t1.setVisible(false);
		}else {
			t1.setVisible(true);
		}
		if(role.getCareer().getSkills().get(1).getAvailability() != 0) {
			t2.setVisible(false);
		}else {
			t2.setVisible(true);
		}
		if(role.getCareer().getSkills().get(2).getAvailability() != 0) {
			t3.setVisible(false);
		}else {
			t3.setVisible(true);
		}
		if(role.getCareer().getSkills().get(3).getAvailability() != 0) {
			t4.setVisible(false);
		}else {
			t4.setVisible(true);
		}
		if(role.getCareer().getSkills().get(4).getAvailability() != 0) {
			t5.setVisible(false);
		}else {
			t5.setVisible(true);
		}
	}

	// 输出战斗信息
	public void outputFightMsg(Mypanel mp, Role role, JTextArea info, Role enemy) {
		
		dp.startDualEnemy(role, enemy);  
		dp.endRound(role, enemy);
	

	}

	public void sendData(String str, JPanel panel,  DataOutputStream dos, DataInputStream dis) {
        try {
            dos.writeUTF(str);
            dos.flush();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "发送数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }
	}
}